package com.bootdo.act.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.act.domain.InviteRecordDO;
import com.bootdo.act.service.InviteRecordService;
import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 邀请记录
 * 
 * @author kwx
 * @email 904358838@qq.com
 * @date 2017-12-19 16:01:19
 */
 
@Controller
@RequestMapping("/act/inviteRecord")
public class InviteRecordController extends BaseController{
	@Autowired
	private InviteRecordService inviteRecordService;
	
	@GetMapping()
	@RequiresPermissions("act:inviteRecord:inviteRecord")
	String InviteRecord(){
	    return "act/inviteRecord/inviteRecord";
	}
	
	private static String InviteRecorded = "InviteRecorded";
	private static String InviteRecord = "InviteRecord";
	
	/**
	 * 被邀请的页面
	 * @return
	 */
	@GetMapping("/InviteRecorded")
	@RequiresPermissions("act:inviteRecord:inviteRecord")
	String InviteRecorded(){
	    return "act/inviteRecord/InviteRecorded";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("act:inviteRecord:inviteRecord")
	public PageUtils list(@RequestParam Map<String, Object> params){
		if(InviteRecorded.equals(params.get("type"))){
			params.put("userId", getUserId());
		}else if(InviteRecord.equals(params.get("type"))){
			params.put("createrId", getUserId());
		}
		
		//查询列表数据
        Query query = new Query(params);
		List<InviteRecordDO> inviteRecordList = inviteRecordService.list(query);
		int total = inviteRecordService.count(query);
		PageUtils pageUtils = new PageUtils(inviteRecordList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("act:inviteRecord:add")
	String add(){
	    return "act/inviteRecord/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("act:inviteRecord:edit")
	String edit(@PathVariable("id") Long id,Model model){
		InviteRecordDO inviteRecord = inviteRecordService.get(id);
		model.addAttribute("inviteRecord", inviteRecord);
	    return "act/inviteRecord/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("act:inviteRecord:add")
	public R save( InviteRecordDO inviteRecord){
		if(inviteRecordService.save(inviteRecord)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("act:inviteRecord:edit")
	public R update( InviteRecordDO inviteRecord){
		inviteRecordService.update(inviteRecord);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("act:inviteRecord:remove")
	public R remove( Long id){
		if(inviteRecordService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("act:inviteRecord:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		inviteRecordService.batchRemove(ids);
		return R.ok();
	}
	
	/**
	 * 邀请回应
	 */
	@PostMapping( "/reply")
	@ResponseBody
	@RequiresPermissions("act:inviteRecord:edit")
	public R reply(Long id,int isAgree){
		InviteRecordDO recordDO = inviteRecordService.get(id);
		if(recordDO != null){
			recordDO.setIsAgree(isAgree);
			recordDO.setReplyDate(new Date());
			if(inviteRecordService.reply(recordDO)>0){
				return R.ok();
				}
				return R.error();
		}
		return R.error();
	}
	
}
